const { getPool } = require('../config/db')

class Model {
  static async create(role_id, menu_id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('INSERT INTO role_menu_relations (role_id, menu_id) VALUES (?,?)', [role_id, menu_id])
    return rows.insertId
  }

  static async update(role_id, menu_id, id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('UPDATE role_menu_relations SET role_id =?, menu_id =? WHERE id =?', [role_id, menu_id, id])

    return rows
  }

  static async removeById(id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('DELETE FROM role_menu_relations WHERE id = ?', [id])
    return rows
  }

  static async removeByRoleId(role_id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('DELETE FROM role_menu_relations WHERE role_id = ?', [role_id])
    return rows
  }

  static async findById(id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM role_menu_relations WHERE id =?', [id])
    return rows[0]
  }

  static async findAll() {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM role_menu_relations', [])
    return rows
  }
}

module.exports = Model
